package com.igeek.service;

import com.igeek.dao.ProductDao;
import com.igeek.domain.Page;
import com.igeek.domain.Product;

import java.sql.SQLException;
import java.util.List;

public class ProductService {
    private ProductDao productDao=new ProductDao();

    public List<Product> getHotproduct(){
        try {
            return productDao.findHotProduct();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Product> getNewproduct(){
        try {
            return productDao.findNewProduct();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 分页查询
     * @param cid
     * @param currrentPage
     * @return
     */
    public Page getProductByCid(String cid,int currrentPage){
        try {
            //总条数
            int total=productDao.getCountByCid(cid);
            //每页展示多少条
            int size=12;
            //总页数
            int count=(total%size==0)?(total/size):(total/size+1);
            //当前页展示的数据
            int start=(currrentPage-1)*size;
            List<Product> productList=productDao.findProductByCid(cid,start,size);
            //构建page对象
            Page page=new Page(total,size,count,currrentPage,productList);
            return page;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Product getProductByPid(String pid){
        try {
            return productDao.findProductByPid(pid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
